<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		protected $fang;
		protected $config;
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			parent::__construct();
			$this->getConfig();
			set_time_limit(0);
		}
		/**
		 * 获取配置项
		 * 
		 */
		public function getConfig(){
			$sql = "select * from yang_config";
			$config = $this->select($sql);
			$configLast = [];
			foreach($config as $key=>$value){
				$configLast[$value['key']] = $value['value'];
			}
			$this->config = $configLast;
		}
		//开始自动程序
		public function run(){
			$level_arr=[];
			$sql="select * from yang_price";
			$pricearr=$this->find($sql);
			$sql="select u.* from yang_member as m left join yang_user_relation as u on m.member_id = u.uid";
			$list=$this->select($sql);
			$relationarr=[];
			foreach($list as $k=>$v){
				$yeji=$v['num1']*$pricearr['price1']+$v['num2']*$pricearr['price2']+$v['num3']*$pricearr['price3']+$v['num4']*$pricearr['price4'];
				$relationarr[$v['uid']]= [
							'pid'=>$v['pid'],
							'p_num'=>$v['p_num'],
							'yeji'=>$yeji,
							'num1'=>0,
							'num2'=>0,
							'num3'=>0,
							'num4'=>0,
							'level'=>0,
						];
			}
			foreach($relationarr as $k=>&$v){
					$level=0;
					if($v['yeji']>=200000&&$v['p_num']>=8){
						$relationarr[$v['pid']]['num1']++;
						$level=1;
					}
					if($v['num1']>=3){
						$relationarr[$v['pid']]['num2']++;
						$level=2;
					}
					if($v['num2']>=3){
						$relationarr[$v['pid']]['num3']++;
						$level=3;
					}
					if($v['num3']>=3){
						$relationarr[$v['pid']]['num4']++;
						$level=4;
					}
					if($v['num4']>=3){
						$level=5;
					}
					$level_arr[] = [
							'member_id'=>$k,
							'level'=>$level,
						];			
			}
		if(!empty($level_arr)){
			$this->update('yang_member', 'member_id', 'level', $level_arr);
		}	
	}
	}
	$auto=new auto();
	$auto->run();
	
?>